package edu.uc.ui;

import com.foodview1.R;

import edu.uc.dto.Review;
import edu.uc.persistence.ReviewDAO;
import edu.uc.persistence.ReviewDAOInterface;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * ReviewFoodActivity class
 * @author Ckorczyk1
 * 
 * This class handles all of the buttons and controls that are displayed 
 * on the ReviewFood.xml page.
 *
 */
public class ReviewFoodActivity extends MainActivity {

	private static final int CAMERA_PIC_REQUEST = 2500;
	private LocationManager locationManager;
	private String provider;
	private int lon;
	private int lat;

	private Button btnBackfromReview;
	private Button btnSubmitReview;
	private Button btnLocation;
	private Button btnPhotoReview;

	//(Robert) Declare the create object
	ReviewDAOInterface create;
	/**
	 * onCreateMethod
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.review_food);
		setTitle("Food View");

		// Call Page Initializer
		init();
	}
	
	/**
	 * init method
	 * 
	 */
	public void init() {

		// Initialize the context
		create = new ReviewDAO(this);
		
		// Define page variables
		btnBackfromReview = (Button)findViewById(R.id.btnBackFromReview);
		btnSubmitReview = (Button)findViewById(R.id.btnSubmitReview);
		btnLocation = (Button)findViewById(R.id.btnLocation);
		btnPhotoReview = (Button)findViewById(R.id.btnTakePhoto);

		// Register the onClick listener
		btnBackfromReview.setOnClickListener(new OnClickListener() {

			/**
			 * onClick method
			 */
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent clickLogin = new Intent(v.getContext(), MainActivity.class);
				startActivityForResult(clickLogin, 0);
				finish();
			}
		});


		// Register the onClick listener
		btnSubmitReview.setOnClickListener(new OnClickListener() {

			/**
			 * onClick method
			 */
			@Override
			public void onClick(View v) {

				// Save the user to DB
				saveNewReview();

				// Inform the user
				Toast.makeText(context, "Review has been saved!", Toast.LENGTH_LONG).show();

				// Continue to main screen
				Intent clickSubmit = new Intent(v.getContext(), MainActivity.class);
				startActivityForResult(clickSubmit, 1);
				finish();
			}
		});


		//Add location to the review when this button is pressed
		btnLocation.setOnClickListener(new OnClickListener() {

			/**
			 * onClick method
			 */
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// Get location the easiest way possible
				locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				Criteria crit = new Criteria();

				provider = locationManager.getBestProvider(crit, false);
				Location location = locationManager.getLastKnownLocation(provider);

				//hard code some locations
				lat = 0;
				lon = 0;
				if (location != null) {
					lat = (int) (location.getLatitude()  *1E6);
					lon = (int) (location.getLongitude()  *1E6);
					// Save to database
					popup("Location saved! \nLat: " + lat + "\nLong: " + lon);
				}else{
					popup("Location not found.");
				}
			}
		});

		// Take care of the camera button
		btnPhotoReview.setOnClickListener(new OnClickListener() { // Register the onClick listener
			@Override
			public void onClick(View v) {
				Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
			}
		});

	}
	
	protected void saveNewReview() {
		Log.i(getClass().getName(), "Saving new review...");
		Review NewReview = new Review();

		// Define NewUser variables
		NewReview.setUserId("Session User ID");
		NewReview.setPictureId("Picture ID");
		NewReview.setRestaurantName("Restaurant Name");
		NewReview.setRestaurantReview("tbReview.getText().toString()");
		NewReview.setRestaurantRating("Restaurant Rating");

		try {
			// Save the new user
			create.save(NewReview);
		} catch (Exception e) {
			Log.e(getClass().getName(), "Error saving new review: " + e.getMessage());
			Toast.makeText(this, "Error saving new review: " + e.getMessage(), Toast.LENGTH_LONG).show();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Log.i(getClass().getName(), "Review has been Saved!");
	}
	
	//
	//	 @Override
	//	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//	        if (requestCode == CAMERA_PIC_REQUEST) {
	//	              Bitmap image = (Bitmap) data.getExtras().get("data");
	//	              ImageView imageview = (ImageView) findViewById(R.id.imageView1);
	//	              imageview.setImageBitmap(image);
	//	        }
	//	    }

}